package cn.edu.pku.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.core.io.IntegerBoundsEnforcer;

import cn.edu.pku.entity.Borrower;
import cn.edu.pku.mapper.BorrowerMapper;
import cn.edu.pku.vo.request.BorrowerRequest;

@Service
public class BorrowerService {

	@Autowired
	BorrowerMapper borrowerMapper;

	public Boolean saveBorrower(BorrowerRequest borrowerRequest) {
		Borrower borrower=new Borrower( borrowerRequest.getBorrowerName(), borrowerRequest.getGender(),
				borrowerRequest.getCellphone(),borrowerRequest.getEmail(),borrowerRequest.getRelationship(),
				borrowerRequest.getRecord(), new Date(), new Date());
		int insertSelective = borrowerMapper.insertSelective(borrower);
		if (insertSelective > 0) {
			return true;
		} else {
			return false;
		}
	}

	public Boolean delBorrower(Integer id) {
		int deleteByPrimaryKey = borrowerMapper.deleteByPrimaryKey(id);
		if (deleteByPrimaryKey > 0) {
			return true;
		} else {
			return false;
		}
	}

	public Boolean update(Borrower borrower) {
		borrower.setuTime(new Date());
		int updateByPrimaryKey = borrowerMapper.updateByPrimaryKey(borrower);
		if (updateByPrimaryKey > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public PageInfo<Borrower>  getBorrowerList(Integer pageNow,Integer pageSize,String borrowerName){
		PageHelper.startPage(pageNow,pageSize);
		List<Borrower> borrowerList = borrowerMapper.getBorrowerList(borrowerName);
		PageInfo<Borrower> pageInfo = new PageInfo<Borrower>(borrowerList);
		return pageInfo;
	}
	
	public Borrower getBorrowerById(Integer id){
		return borrowerMapper.selectByPrimaryKey(id);
	}

}
